package cn.maxwell.config;

import cn.maxwell.filter.RequestTimeFilter;
import org.springframework.cloud.gateway.route.RouteLocator;
import org.springframework.cloud.gateway.route.builder.RouteLocatorBuilder;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

@Configuration
public class FilterConfig {

    //配置Filter作用于那个访问规则上
    @Bean
    public RouteLocator customerRouteLocator(RouteLocatorBuilder builder) {

        //访问路径：localhost:1060/users/user/loadById/1
        return builder.routes().route(r -> r.path("/users/**")
                //去掉2个前缀
                .filters(f -> f.stripPrefix(1)
                        .filter(new RequestTimeFilter())
                        .addResponseHeader("X-Response-test", "test"))
                .uri("lb://user-server")
                .order(0)
                .id("test-RequestTimeFilter")//唯一标识，不可重复
        ).build();
    }
}